﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Heyu.Utility
{
    public class EmailUtility
    {
        /// <summary>   
        /// 发送邮件   
        /// </summary>    
        /// <param name="from">发送人邮件地址</param>    
        /// <param name="smtpHost">SMTP服务器地址</param>   
        /// <param name="userName">用户名</param>    
        /// <param name="password">密码</param>    
        /// <param name="isBodyHtml">是否是Html</param>    
        /// <param name="to">接收人邮件地址</param>    
        /// <param name="subject">邮件主题</param>    
        /// <param name="body">邮件体</param>    
        /// <returns>是否成功</returns>    
        public static bool SendEmail(string from, string smtpHost, string username, string password, bool isBodyHtml, string to, string subject, string body)
        {
            string[] ts = to.Split(',');

            MailMessage mm = new MailMessage();
            mm.From = new MailAddress(from);
            foreach (string t in ts)
            {
                mm.To.Add(new MailAddress(t.Trim()));
            }
            mm.Subject = subject;
            mm.IsBodyHtml = isBodyHtml;
            mm.Body = body;
            SmtpClient sc = new SmtpClient();
            sc.Host = smtpHost;
            sc.UseDefaultCredentials = true;//winform中不受影响，asp.net中，false表示不发送身份严正信息    
            sc.EnableSsl = false;
            //如果服务器不支持ssl则报，服务器不支持安全连接 错误                
            sc.Credentials = new System.Net.NetworkCredential(username, password);
            sc.DeliveryMethod = SmtpDeliveryMethod.Network;
            sc.Send(mm);
            return true;
        }


    }
}
